<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class analysisAction extends BackAction{
    
    private $users      = array();
    private $weibo_id   = null;

    
    function _ex($obj){
        if(array_key_exists('error', $obj) && array_key_exists('error_code', $obj)){
            echo $obj['error'];
            exit;
        }
        return $obj;
    }
    /**
     * 性别分析
     */
    private function _sex(){
        
        foreach ($this->users as $k1 => $v1) {
            $g  = $v1['gender'];
            $gender[$g][]  = $g;
        }
        
        foreach ($gender as $k2 => $v2) {
            DB_SEX::saveSex($this->weibo_id, $k2, count($v2));
        }
        
    }
    
    /**
     * 地域分析
     */
    function _location(){
        
        $loc    = array();
        
        foreach ($this->users as $k1 => $v1) {
            if(empty($v1['location'])){
                $loc['未知'][]  = '未知';
                continue;
            }
            $location   = explode(' ', $v1['location']);
            $loc[$location[0]][]    = $v1['location'];
        }
        
        foreach ($loc as $k3 => $v3) {
            DB_LOCATION::saveLocation($this->weibo_id, $k3, count($v3));
        }
    }

    
    function actionIndex(){
        $this->setPageTitle('高级僵尸粉检测程序');
        $this->render();
    }
    
    /**
     * 高级僵死粉丝分析系统
     */
    function actionProcess(){
        $ids    = $this->postVar('ids');
        if($ids){
            $urls   = explode(' ', $ids);
            //$weibo  = new WeiboLogin('x25125x@126.com', 'dehua2011', '3_5bc7d0e414cf2f2b6eb23f135ed802c1f4f6fb17', '5c56', '2106492435');
            $gsid   = '3_5bc7d0e414cf2f2b6eb23f135ed802c1f4f6fb17';

            //$urls   = array('http://weibo.cn/dpool/ttt/home.php?filter=0&uid=2420874747&vt=4&gsid=3_5bc7d0e414cf2f2b6eb23f135ed802c1f4f6fb17');
            //$urls   = array(1877002380);
            $spider = new Spider($urls);
            $spider->start();
            $user   = $spider->get_users();
            $_SESSION['user']   = $user;
        }
        $this->redirect('analysis');
    }
    
    /**
     * 获取用户粉丝
     */
    function actionFollows(){
        $gender = array();
        $weibo_id   = '1266321801';
        $this->weibo_id = $weibo_id;
        $oauth  = ORM_Finder::findOne('DB_OAUTH');
        $token  = $oauth->getToken();
        
//        $c  = new SaeTClientV2(WB_AKEY, WB_SKEY, $token);
//        
//        $re = $this->_ex($c->followers_by_id($weibo_id, 0, 200));
//        
//        $this->set_users($re['users']);
//        //保存
//        DB_FOLLOWS::batchSave($weibo_id, $re['users']);
//        
//        while($re['next_cursor']){
//            $re = $this->_ex($c->followers_by_id($weibo_id, $re['next_cursor'], 200));
//            //保存
//            DB_FOLLOWS::batchSave($weibo_id, $re['users']);
//            
//            $this->set_users($re['users']);
//        }
//        $_SESSION['user_arr']   = $this->users;
        
        $this->users    = $_SESSION['user_arr'];
        //print_r($this->users);
        $this->_location();
        echo 'ok';
    }
    
    //获取用户微博内容
    function actionBlog(){
        $token  = $this->getToken();
        $weibo_id   = '2350462347';
        $c  = new SaeTClientV2(WB_AKEY, WB_SKEY, $token);
        $re = $this->_ex($c->user_timeline_by_id($weibo_id, 1, 200));
        while($re['next_cursor']){
            $re = $this->_ex($c->user_timeline_by_id($weibo_id, 1, 200, $re['next_cursor']));
        }
    }
    
    /**
     * 批量分析
     */
    public function actionRun() {
        /**
         * record at 2011-10-25 11:41 30a2c6069c44a7efde760013accd015b
         */

        $token   = 'd613f705519398c6d10f67cfe3ef26d8';
        $c  = new SaeTClientV2( WB_AKEY , WB_SKEY , $token);
        $re = $c->followers_by_id('2350462347', 0, 200);
        if(array_key_exists('error', $re) && array_key_exists('error_code', $re)){
            echo $re['error'];
            exit;
        }
        
        $next_cursor    = $re['next_cursor'];
        $total_number   = $re['total_number'];
        while($next_cursor < $total_number){
            $urls[]         = 'https://api.weibo.com/2/friendships/followers.json?uid=1797810723&cursor='.$next_cursor.'&count=200';
            $next_cursor    = $next_cursor + 200;
        }

        $fp = new MultiHttpRequest($urls);
        $fp->set_access_token($token);
        $fp->set_users($re['users']);
        $fp->start();
        $_SESSION['users']  = $fp->get_users();
        echo count($_SESSION['users']);
        print_r($_SESSION['users']);
        exit;
    }
    
    function set_users($users){
        foreach ($users as $k => $v) {
            $this->users[]  = $v;
        }        
    }
}
?>
